﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Display;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Util.Management;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ADF.Connection.Local;
using System.Drawing;

namespace PlanningGIS.Draw
{
    public partial class DropDownFillColor : DropDownStyleColor, ICommand
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DropDownFillColor()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
            Init();

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container">控件容器</param>
        public DropDownFillColor(IContainer container)
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            container.Add(this);

            InitializeComponent();

            Init();
        }

        /// <summary>
        /// 系统框架
        /// </summary>
        IApplication m_Application = null;

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            base.InitStyle();
            string imgname2 = this.GetType().Namespace + ".Resources.FillColor.png";
            Bitmap bmp2 = new Bitmap(GetType().Assembly.GetManifestResourceStream(imgname2));
            bmp2.MakeTransparent();
            base.ImageShape = bmp2;
            base.ToolTip = "填充色设置";
            base.SelectedColor = Color.LightYellow;
            
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void OnSelectColor()
        {
            base.OnSelectColor();
            IRgbColor rgbColor = null;
            if (base.SelectedColor == Color.Transparent)
            {
                rgbColor = new RgbColorClass();
                rgbColor.NullColor = true;
            }
            else
            {
                rgbColor = Converter.ToRGBColor(base.SelectedColor);
            }
            (m_Application.Document as IMxDocument).set_DefaultColor(esriMxDefaultColorTypes.esriMxFillColor, rgbColor);
            IActiveView m_ActiveView = (m_Application.Document as IMxDocument).ActiveView;
            IGraphicsContainerSelect pGraphicsContainerSelect = m_ActiveView as IGraphicsContainerSelect;
            if (pGraphicsContainerSelect != null)
            {
                IEnumElement pEnumElement = pGraphicsContainerSelect.SelectedElements;
                IElement pElement = pEnumElement.Next();
                while (pElement != null)
                {
                    if (pElement is IFillShapeElement)
                    {
                        IFillShapeElement pFillShapeElement = (IFillShapeElement)pElement;
                        IFillSymbol pFillSymbol = pFillShapeElement.Symbol;
                        pFillSymbol.Color = rgbColor;
                        // Converter.ToRGBColor(this.pColor);
                        // (m_Application.Document as IMxDocument).get_DefaultColor(esriMxDefaultColorTypes.esriMxFillColor);
                        ILineSymbol pLineSymbol = pFillSymbol.Outline;
                        pLineSymbol.Color = (m_Application.Document as IMxDocument).get_DefaultColor(esriMxDefaultColorTypes.esriMxLineColor);
                        pFillSymbol.Outline = pLineSymbol;
                        pFillShapeElement.Symbol = pFillSymbol;
                    }
                    pElement = pEnumElement.Next();
                }
            }
            m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }
        #region ICommand Members

        int ICommand.Bitmap
        {
            get { return 0; }
        }

        string ICommand.Caption
        {
            get { return "DropDownMxColor"; }
        }

        string ICommand.Category
        {
            get { return "颜色设置"; }
        }

        bool ICommand.Checked
        {
            get { return false; }
        }

        bool ICommand.Enabled
        {
            get
            {
                if (m_Application == null)
                    return false;
                return (m_Application as IFramework).IsSceneCtlActive == false;
            }
        }

        int ICommand.HelpContextID
        {
            get { return 0; }
        }

        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }

        string ICommand.Message
        {
            get { return "DropDownMxColor"; }
        }

        string ICommand.Name
        {
            get { return "DropDownMxColor"; }
        }

        void ICommand.OnClick()
        {

            if (m_Application != null)
            {
                //IColor m_Color = (m_Application.Document as IMxDocument).get_DefaultColor(esriMxDefaultColorTypes.esriMxFillColor);
                //this.btnLeft.BackColor = Converter.FromRGBColor(m_Color as IRgbColor);
            }
        }

        void ICommand.OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        string ICommand.Tooltip
        {
            get
            {
                return "填充色设置";
            }
        }

        #endregion

    }
}
